package com.cn.servlet.book;

import com.cn.Dao.*;
import com.cn.base.Util;
import com.cn.entity.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.*;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@WebServlet(name = "BookDetailServlet")
public class BookDetailServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String state=request.getParameter("state");
        String id=request.getParameter("button");
        System.out.println(state+id);
        BookDao bookDao=new BookDao();
        bookDao.UpdateById(Integer.valueOf(id),Integer.valueOf(state));
        request.getSession().setAttribute("idd",id);
        this.doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            String id=request.getParameter("id");
            if(id==null)id= (String) request.getSession().getAttribute("idd");
            System.out.println("id:"+id);
            List<String> book=new ArrayList<>();
            NewsinfoDao newsinfoDao=new NewsinfoDao();
            NewsclassDao newsclassDao=new NewsclassDao();
            BookDao bookDao=new BookDao();
            DepDao depDao=new DepDao();
            UserDao userDao=new UserDao();
            BookInfo bi=bookDao.findById(Integer.valueOf(id));
            NewsInfo ni=newsinfoDao.findByNum(bi.getNum());
            Newsclass nc=newsclassDao.findById(ni.getId());
            UserInfo ui=userDao.findById(bi.getUserid());
            DepInfo di=depDao.findById(ui.getDepartmentid());
            List<String> list=new ArrayList<>();

            list.add(new String("订单编号"));
            list.add(String.valueOf(bi.getBid()));
            list.add("客户姓名");list.add(ui.getName());
            list.add("用户名");list.add(ui.getUsername());
            list.add("所属部门");list.add(di.getDname());
            list.add("报刊名称");list.add(ni.getName());
            list.add("报刊编号");list.add(ni.getNum());
            list.add("报刊类别");list.add(nc.getNclass());
            list.add("订购份数");list.add(String.valueOf(bi.getAmount()));
            list.add("订购月数");list.add(String.valueOf(bi.getMouth()));
            list.add("订单总额");list.add(String.valueOf(bi.getTotal_price()));
            list.add("订单状态");
            if(bi.getState()==0)
                list.add("拒绝");
            else if (bi.getState()==1){
                list.add("接受");
            }
            else list.add("未处理");
            list.add("创建时间");list.add(Util.changeDate(bi.getTime()));
            System.out.println("bookDetali加载成功");
            request.getSession().setAttribute("list",list);
            request.getRequestDispatcher("bookDetail.jsp").forward(request, response);

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

    }
}
